package com.zjitc.bookshop39.controller;


import com.zjitc.bookshop39.entity.Book;
import com.zjitc.bookshop39.service.BookService;
import com.zjitc.bookshop39.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
*@Author:徐莹
*@Date:2025/04/28 13:52:00
*/


@RestController
public class BookController {
    @Autowired
    private BookService bookService;
    /**
     * 查询所有book
     */
    @PreAuthorize("hasAnyAuthority('sys:book:selectall')")
    @GetMapping("/book")
    public ApiResponse<List<Book>> getBooksList() {
        List<Book> bookList = bookService.getBooksList();
        if (bookList != null) {
            return ApiResponse.success("Success",bookList);
        }
        return ApiResponse.error("查询失败或没有图书列表");
    }

    /**
     * 根据ID查询book
     */
    @PreAuthorize("hasAnyAuthority('sys:book:select')")
    @GetMapping("/book/{id}")
    public ApiResponse<Book> getBookById(@PathVariable int id) {
        Book product =bookService.getBookById(id);
        if (product != null) {
            return ApiResponse.success("Success",product);
        } else {
            return ApiResponse.error("查询失败或没有该图书");
        }
    }

}
